﻿
using Dal;
using IBll;
using IDal;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bll
{
    /// <summary>
    /// /房号接口
    /// </summary>
    public class StarePteabLL : IStarePteabLL
    {
        /// <summary>
        /// 实例化
        /// </summary>
        /// <param name="stateDa"></param>
         public StarePteabLL(IStateDal<StarePtea> stateDa, IStateDal<Stare> stateD)
        {
            StateDa = stateDa;
            StateD = stateD;
        }

        public IStateDal<StarePtea> StateDa { get; }
        public IStateDal<Stare> StateD { get; }

        /// <summary>
        /// 新增房号信息
        /// </summary>
        /// <param name="starePtea">房号表添加数据</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddAnyns(StarePtea starePtea)
        {
            try//异常处理
            {
                ///通过房号表查询数据
               var lisye = StateDa.GetValues().FirstOrDefault(x => x.StarePteaName == starePtea.StarePteaName);
                if (lisye==null)
                {
                    ///
                      return await StateDa.AddAnyns(starePtea);

                }
                return -1;//房号已存在
            }
            catch (Exception)
            {

                throw;
            }
         
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <returns>返回受影响行数</returns>
        public List<StarePtea> GetStarePteas()
        {
            try//异常处理
            {
                 return StateDa.GetValues().ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns>通过id 获取一条信息</returns>

        public async Task<int> Isdelete(int id)
        {
            try//异常处理
            {
                //通过id获取一条数据
                var p = StateDa.GetValues().FirstOrDefault(x=>x.StarePteaId== id);
                p.Idelete = 1;

                 return await StateDa.UpdateAnyns(p);


            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 逻辑修改
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns>通过id 获取一条信息</returns>
        public async Task<int> ISstate(int id)
        {

            try//异常处理
            {
                //通过id获取一条数据
               var p = StateDa.GetValues().FirstOrDefault(x => x.StarePteaId == id);

                p.IsStarePtea = false;

               

                 return await StateDa.UpdateAnyns(p);



            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 查询分页
        /// </summary>
        /// <param name="name">房号名称</param>
        /// <param name="starePteaDate">创建时间</param>
        /// <param name="index">页数</param>
        /// <param name="size">条数</param>
        /// <returns>通过房号名称房号名称页数条数查询一条数</returns>

        public dynamic ShowStarePtea(string? name, DateTime? starePteaDate, DateTime? endStarePtea)
        {
            var starePtea = StateDa.GetValues();
            var stare=StateD.GetValues();
            var linq = from a in starePtea
                       join b in stare
                       on a.StarePteaId equals b.StarePteaId

                       select new
                       {
                           a.StarePteaName,
                           a.StarePteaType,
                           a.StarePteaDate,
                           a.StarePteaId,
                           a.IsStarePtea,
                            b.StareId,
                           a.EndStarePtea
                            
                       };

            if (!string.IsNullOrEmpty(name))
            {
                linq = linq.Where(x => x.StarePteaName.Contains(name));
            }
            if (starePteaDate!=null)
            {
                linq = linq.Where(x => x.StarePteaDate >= starePteaDate);

            }

            if (endStarePtea != null)
            {
                linq = linq.Where(x => x.EndStarePtea <= endStarePtea);

            }

            return linq.ToList();




        }

    }
}
